package com.example.weiting.huishenghuo.com.example.myapplication;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

import com.example.weiting.huishenghuo.R;
import com.example.weiting.huishenghuo.utils.Message;
import com.example.weiting.huishenghuo.utils.MessageAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Select extends AppCompatActivity {
    Button bt,bt1;
    int year ;
    int month ;
    int day ;
    long startTime,overTime;
    TextView tv,tv1;
    List<Message> messageList = new ArrayList<Message>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        tv = (TextView) findViewById(R.id.textView15);
        tv1 = (TextView) findViewById(R.id.textView17);

        //获取messageList集合
        Intent intent = getIntent();
        Bundle args = intent.getBundleExtra("messages");
        messageList = (ArrayList<Message>) args.getSerializable("messageList");

        //通过按钮获取开始时间
        bt = (Button) findViewById(R.id.button6);
        bt.setText("请设置开始时间");
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

           DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
               @Override

               public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                   year = i;
                   month = i1;
                   day = i2;
                   Calendar calendar = Calendar.getInstance();
                   calendar.set(year,month,day,0,0,0);
                   Date date = calendar.getTime();
                   bt.setText(date.toString());
                   startTime = date.getTime();
               }
           };
           DatePickerDialog dialog = new DatePickerDialog(Select.this,listener, Calendar.getInstance().get(Calendar.YEAR),Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DATE));
           dialog.show();
            }
        });

        //通过按钮获取终止时间
        bt1 = (Button) findViewById(R.id.button7);
        bt1.setText("请设置终止时间");
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
                    @Override

                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        year = i;
                        month = i1;
                        day = i2;
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(year,month,day,0,0,0);
                        Date date = calendar.getTime();
                        bt1.setText(date.toString());
                        overTime = date.getTime();
                    }
                };
                DatePickerDialog dialog = new DatePickerDialog(Select.this,listener, Calendar.getInstance().get(Calendar.YEAR),Calendar.getInstance().get(Calendar.MONTH),Calendar.getInstance().get(Calendar.DATE));
                dialog.show();
            }
        });

        //获取查询按钮开始查询
        Button bt2 = (Button) findViewById(R.id.button8);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int spend = 0;
                int zhuan = 0;
                List<Message> messages = new ArrayList<>();
                //遍历messageList，判断是否符合情况
                for (Message message: messageList) {
                    if (message.getDate().getTime() >= startTime && message.getDate().getTime() <= overTime){
                        messages.add(message);
                        if (message.getZhuangTai() < 0) {
                            spend += message.getZhuangTai();
                        } else {
                            zhuan += message.getZhuangTai();
                        }
                    }
                }
                //显示ListView中的数据。
                MessageAdapter adapter = new MessageAdapter(Select.this, R.layout.list_item, messages);
                ListView messagesView = (ListView) findViewById(R.id.cmc1);
                messagesView.setAdapter(adapter);
                tv1.setText(String.valueOf(zhuan));
                tv.setText(String.valueOf(Math.abs(spend)));
            }
        });
    }
}
